<template>
  <div class="sumbit_container">
    <order-address
    :addressInfo="addressInfo"
    :id="addressInfo.id"
    ></order-address>
    <div class="com">
      <div class="com_td">共{{list.length}}件商品</div>
      <ul class="com_dd">
        <li v-for="(item, i) in list" :key="i">
          <a>
            <order-item
              :id="item.id"
              :productInfo="item.productInfo"
              :cart_num="item.cart_num"
            ></order-item>
          </a>
        </li>
      </ul>
    </div>
   

   <submit-info
   :priceGroup="priceGroup"
   :integral="userInfo.integral"
   @share-isJS="getCost"
   @share-inp="getText"
   ></submit-info>
   
    <pay-type
    :now_money="userInfo.now_money"
    @share-pagtype="getPagtype"
    ></pay-type>

    <pay-price
    :result="result"
    >

    </pay-price>

    <pay-count
    :orderKey="orderKey"
    :id="addressInfo.id"
    :totalPrice="result.pay_price"
    :payType="payType"
    :useIntegral="useIntegral"
    :mask="mask"
    ></pay-count>
  </div>
</template>

<script>
import OrderItem from "@/views/order/submit/OrderItem.vue";
import OrderAddress from "@/views/order/submit/Address.vue";
import SubmitInfo from "@/views/order/submit/SubmitInfo.vue";
import PayType from "@/views/order/submit/PayType.vue";
import PayPrice from "@/views/order/submit/PayPrice.vue";

import PayCount from "@/views/order/submit/PayCount.vue";
export default {
  name: "Submit",
  data() {
    return {
      list: [],//商品

      addressInfo:{}, // 地址

      priceGroup:{},
      
      userInfo:{},
      orderKey:"",

      result:{},
      payType:"yue",
      useIntegral:0, // 优惠卷
      shipping_type:1,
      mask:""
    };
  },
  created() {
   
    this.initUser();
    // this.initComputed();
    this.initConfirm();
    // this.initPriceData()
  },
  methods: {
   
    // 用户信息
    initUser() {
      let url = "/api/user";
      let token = localStorage.getItem("token");
      let data = {
        page: 1,
        limit: 20,
      };
      let pro = axios({
        method: "get",
        url: url,

        headers: {
          "Authori-zation": "Bearer " + token,
        },
      })
        .then((res) => {
          let { status, data } = res.data;
          if (status == 200) {
            this.nowMoney = data.now_money;
            this.userId = data.uid
            this.integral = data.integral
            
          }
          console.log("用户数据获取成功", res.data);
        })
        .catch((err) => {
          console.log("用户数据失败");
        });
    },

    //
    initConfirm() {
      let url = "/api/order/confirm";
      let token = localStorage.getItem("token");
      let data = {
        cartId:this.$route.params.id
      };
      let pro = axios({
        method: "post",
        url: url,
        data:data,
        headers: {
          "Authori-zation": "Bearer " + token,
        },
      })
        .then((res) => {
          let { status, data } = res.data;
          if (status == 200) {
            this.addressInfo = data.addressInfo

            this.list = data.cartInfo

            this.priceGroup = data.priceGroup

            this.userInfo = data.userInfo

            this.orderKey = data.orderKey
           
          }
          
          console.log("获取成功", res.data);

          if(this.orderKey ){
            this.initPriceData(this.orderKey)
          }
          
        })
        .catch((err) => {
          console.log("数据失败");
        });
    },

    initPriceData(key){
      let url = `/api/order/computed/`+key;
      let token = localStorage.getItem("token");
      let data = {
        addressId:3682,
        couponId:0,
        payType:this.payType,
        shipping_type:this.shipping_type,
        useIntegral:this.useIntegral,
      };
      let pro = axios({
        method: "post",
        url: url,
        data:data,
        headers: {
          "Authori-zation": "Bearer " + token,
        },
      })
        .then((res) => {
       
          let { status, data } = res.data;
          if (status == 200) {
            this.result = data.result
            console.log("获取成功", data);
          }
          
          
        })
        .catch((err) => {
          console.log("数据失败");
        });
    },

    getCost(val){
      this.useIntegral = val
      this.initPriceData(this.orderKey)
    },

    getPagtype(val){
      this.payType = val
      this.initPriceData(this.orderKey)
    },

    getText(val){
      this.mask = val
      console.log("666",this.mask)
    },
    
    //点击

    shareFlag(val){
      this.isflag = val
    }
  },
  components: {
    OrderItem,
    OrderAddress,
    PayType,
    SubmitInfo,
    PayCount,
    PayPrice,

  },
};
</script>

<style lang="scss" scoped>
.sumbit_container {
  padding-bottom: 60px;
}
.com {
  background-color: #fff;
  .com_td {
    border-bottom: 1px solid #eee;
    height: 43px;
    padding: 0 15px;
    line-height: 43px;
    font-size: 15px;
    color: #282828;
  }
  .com_dd {
    li {
      padding-left: 15px;
      a {
        display: block;
        padding: 12px 0px;
        border-bottom: 1px solid #eee;
      }
    }
  }
}
.total {
  height: 48px;
  line-height: 48px;
  font-size: 14px;
  color: #282828;
  padding: 0 15px;
  display: flex;
  justify-content: space-between;
  margin: 12px 0px;
  background-color: #fff;
}
</style>
